#include "Util.hpp"
#include "Config.hpp"
#include "DataManager.hpp"
#include "HotManager.hpp"
#include "Server.hpp"
#include <thread>

void TestFileUtil(const std::string& filename)
{
    // hhbcloud::FileUtil fu(filename);

    // std::cout << fu.FileSize() << std::endl;
    // std::cout << fu.LastMTime() << std::endl;
    // std::cout << fu.LastATime() << std::endl;
    // std::cout << fu.FileName() << std::endl;

    // std::string str = "hello world!!!--";
    // fu.SetContent(str);

    // std::string body;
    // fu.GetContent(&body);
    // std::cout << body << std::endl;

    // hhbcloud::FileUtil nfu("./hello.txt");
    // nfu.SetContent(body);


    // hhbcloud::FileUtil fu(filename);
    // fu.CreateDirectory();

    // std::vector<std::string> array;
    // fu.ScanDirectory(&array);

    // for (auto& e : array)
    // {
    //     std::cout << e << std::endl;
    // }
    // std::cout << std::endl;

    hhbcloud::FileUtil fu(filename);

    std::string packname = filename + ".lz";
    fu.Compress(packname);

    std::string unpackname = "hello.txt";
    hhbcloud::FileUtil nfu(packname);
    nfu.Uncompress(unpackname);

}


void TestJsonUtil()
{
    // 序列化

    // 构建一个JSON结构体
    Json::Value root;
    root["姓名"] = "小帅";
    root["年龄"] = 18;
    float score[3] = { 88.5, 66.7, 99.8 };
    for (int i = 0;i < 3; i++)
    {
        root["分数"][i] = score[i];
    }

    // 序列化到str中
    std::string str;
    hhbcloud::JsonUtil::Serialization(root, &str);
    std::cout << str << std::endl;

    // 返序列化
    Json::Value root1;
    hhbcloud::JsonUtil::Deserialization(str, &root1);

    std::cout << root1["姓名"].asString() << std::endl;
    std::cout << root1["年龄"].asInt() << std::endl;
    for (int i = 0;i < root1["分数"].size(); i++)
    {
        std::cout << root1["分数"][i].asFloat() << std::endl;
    }
}


void TestConfig()
{
    hhbcloud::Config* config = hhbcloud::Config::GetInstance();

    std::cout << config->GetHotTime() << std::endl;
    std::cout << config->GetServerIp() << std::endl;
    std::cout << config->GetServerPort() << std::endl;
    std::cout << config->GetDownloadPrefix() << std::endl;
    std::cout << config->GetPackfileSuffix() << std::endl;
    std::cout << config->GetPackDir() << std::endl;
    std::cout << config->GetBackDir() << std::endl;
    std::cout << config->GetBackupFile() << std::endl;
}

void TestDataManager(const std::string& realpath)
{
    hhbcloud::Backinfo info;

    if (info.NewBackinfo(realpath) == false)
    {
        return;
    }

    std::cout << info._pack_flag << std::endl;
    std::cout << info._atime << std::endl;
    std::cout << info._mtime << std::endl;
    std::cout << info._fsize << std::endl;
    std::cout << info._pack_path << std::endl;
    std::cout << info._real_path << std::endl;
    std::cout << info._url_path << std::endl;


    hhbcloud::DataManager DM;

    DM.Insert(info);

    hhbcloud::Backinfo backinfo;
    DM.GetOneByUrl(info._url_path, &backinfo);

    std::cout << "================Insert && GetOneByUrl=====================" << std::endl;

    std::cout << backinfo._pack_flag << std::endl;
    std::cout << backinfo._atime << std::endl;
    std::cout << backinfo._mtime << std::endl;
    std::cout << backinfo._fsize << std::endl;
    std::cout << backinfo._pack_path << std::endl;
    std::cout << backinfo._real_path << std::endl;
    std::cout << backinfo._url_path << std::endl;

    info._pack_flag = true;
    DM.Update(info);
    std::vector<hhbcloud::Backinfo> array;
    DM.GetAll(&array);

    std::cout << "===================Update && GetAll=====================" << std::endl;

    for (auto& e : array)
    {
        std::cout << e._pack_flag << std::endl;
        std::cout << e._atime << std::endl;
        std::cout << e._mtime << std::endl;
        std::cout << e._fsize << std::endl;
        std::cout << e._pack_path << std::endl;
        std::cout << e._real_path << std::endl;
        std::cout << e._url_path << std::endl;
    }

    hhbcloud::Backinfo tmp;
    DM.GetOneByRealpath(realpath, &tmp);

    std::cout << "=================GetOneByRealpath=======================" << std::endl;

    std::cout << tmp._pack_flag << std::endl;
    std::cout << tmp._atime << std::endl;
    std::cout << tmp._mtime << std::endl;
    std::cout << tmp._fsize << std::endl;
    std::cout << tmp._pack_path << std::endl;
    std::cout << tmp._real_path << std::endl;
    std::cout << tmp._url_path << std::endl;

}

hhbcloud::DataManager* data;

void TestHotManager()
{
    data = new hhbcloud::DataManager;
    hhbcloud::HotManager ht;
    ht.RunMoudle();
}

void TestServer()
{
    data = new hhbcloud::DataManager;
    hhbcloud::Service svr;
    svr.RunMoudle();
}

int main(int argc, char* argv[])
{
    // if (argc != 2)
    // {
    //     std::cout << "argv[2]" << std::endl;
    //     return -1;
    // }

    // TestFileUtil(argv[1]);

    // TestJsonUtil();

    // TestConfig();

    // TestDataManager(argv[1]);

    // TestHotManager();
    TestServer();

    // 多线程同时执行，服务器网络通信，业务处理模块和热点管理模块
    // std::thread thread_hot_manager(TestHotManager);
    // std::thread thread_server(TestServer);

    // thread_hot_manager.join();
    // thread_server.join();
    return 0;
}